<?php
if (!defined("OK")) { header("Location: ../"); }
if (isset($url['u']) && isnum($url['u']) && $url['u'] > 0) { $link = (int)$url['u']; } else { $link = 0; }
$p = 0;
if (isset($link) && strlen($link) > 0 && $link > 0) {
	//$db->uzklausa("SELECT * FROM `nuorodos` LIMIT $p,$limit");
	$db->uzklausa("UPDATE `nuorodos` SET click=click+1 WHERE `id`=".escape((int)$link)." LIMIT 1");
	$a = $db->uzklausa("SELECT `url` FROM `nuorodos` WHERE `id`=".escape($link)." LIMIT 1");
	$link = mysql_fetch_assoc($a);
	//redirect($url['u']);
	if (mysql_num_rows($a)>0 && isset($link['url']) && !empty($link['url'])) { redirect($link['url']); }
	else { klaida("Klaida","Tokia nuoroda jau neegzistuoja. Prašome pasitikrinti duomenis"); redirect("?id,9;","meta"); }
}
?>
<table class="main" style="width:100%" align="center" border="0" width="100%">
	<thead>
		<tr>
			<td width="250" nowrap="nowrap" class="lang">Nuoroda</td>
		   <td align="center" class="lang">Paspaudė</td>
		</tr>
	</thead>

	<tbody>
		<tr>
			<td colspan="3" nowrap="nowrap" class="title"> <strong>TOP 5</strong></td>
		</tr>
		<?php #popliariausi
		//$q = $db->uzklausa("SELECT * FROM `nuorodos` ORDER BY `click` DESC LIMIT 0 , 5");
		$q = $db->uzklausa("SELECT `nuorodos`.`id`,`nuorodos`.`url`,`nuorodos`.`click`,`nuorodos`.`date`,`nuorodos`.`apie`,`users`.`nick`
FROM `nuorodos`
Left Join `users` ON `nuorodos`.`nick` = `users`.`id`
ORDER BY `click` DESC LIMIT 0 , 50");
		while($sql = mysql_fetch_assoc($q)) {?>
		<tr>
			<td nowrap="nowrap" class="sarasas">
			<a href="?id,9;u,<?php echo $sql['id']; ?>" title="header=[<?php echo $sql['nick']; ?>] body=[
			<br/><center><img src='<?php echo pic('http://shots.snap.com/preview/?url='.input($sql['url']).'&size=large',427); ?>' border='1' alt='<?php input($sql['url']); ?>' /></center><br/><p>Apie: <i><?php echo wrap1(input($sql['apie']),50); ?></i><br/>Pasiūlė: <b><?php echo input($sql['nick']); ?></b><br/>Data: <b><?php echo $sql['date']; ?> - <?php echo kada($sql['date']); ?></b><br/>Reitingas: <b><?php echo $sql['rank']; ?></b><br/>Paspaudė: <b><?php echo $sql['click']; ?></b></p>] cssbody=[sarasas] cssheader=[title] fade=[on]" target="_blank"><?php echo input($sql['url']); ?></a>
			</td>
		    <td align="center" class="sarasas"><?php echo $sql['click'];?></td>
		</tr>
		<?php } ?>
	</tbody>
</table>
<?php
if (isset($_SESSION['username']) && !empty($_SESSION['username']) && defined("LEVEL") && LEVEL > 0) {
	if (isset($_POST) && !empty($_POST) && $_POST['Submit_link'] == 'Siųsti') {
		// Banned IP addresses 
		/*
		if (file_exists("blockedIPs.txt")) {
			$ip = $_SERVER['REMOTE_ADDR'];
			$fp = fopen("blockedIPs.txt","r"); 
			$banned = fread($fp, 1024*1024);
			fclose($fp);
			$ips = explode("\n", $banned); 
			foreach ($ips as $value) {
				if($ip == trim($value)){
					die("Sorry your IP is banned!"); 
				}
			}
		}
		*/
		// Nustatom kintamuosius
		@$url = strip_tags($_POST['url']);
		@$apie = strip_tags($_POST['apie']);
		@$pic = strip_tags($_POST['pic']);
		
		// Patikrinam
		if (!ereg('http://[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $url)) {
			klaida("Klaida","Prasome patikrinkite ar nuoroda yra teisinga");
			exit;
		}
		elseif (!ereg('http://[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $pic)){
			klaida("Klaida","Prasome patikrinkite ar kelias iki logotipo yra teisingas");
			exit;
		}
		else {
			//Siunciam laiska
			$pfw_header = "From: info@mrcbug.com\n"
			  . "Reply-To: info@mrcbug.com\n";
			$pfw_subject = "Pasiulyta nuoroda";
			$pfw_email_to = "projektas@gmail.com";
			$pfw_message = "Visitor's IP: ".$test['ip']."\n"
			. "Nick: ".$_SESSION['username'] ." ID: ".$_SESSION['id']."\n"
			. "url: $url\n"
			. "apie: $apie\n"
			. "pic: $pic\n"
			. "Prasome patvirtinkite pasiulyta nuoroda";	
			$db->uzklausa("INSERT INTO `nuorodos` (`cat` , `url` , `img` , `nick` , `date` , `apie` ) VALUES ('1', ".escape($url).", ".escape($pic).", ".escape($_SESSION['id']).", NOW(), ".escape($apie).");");
			if (mysql_affected_rows() > 0) { 
				@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;
				msg("Nusiusta","Jūsų nuoroda patalpinta sėkmingai.");
			}
			else { klaida("Klaida","Prasome uzpildykite forma teisingai"); }
		}
	}

	$text = "
 <form name='link_submission' method='POST' action=''>
	<table cellspacing='0' cellpadding='10' border='0' style='width: 100%;' align='center' width='100%'>
	   <tr>
		  <td>
			 <table cellspacing='2' cellpadding='2' border='0'>
				<tr>
				   <td align='right' class='required_field'>
Svetainės URL:
				   </td>
				   <td class='element_label'>

					  <input type='text' name='url' size='20' value='http://' style='width: 200px;'>
				   </td>
				</tr>
				<tr>
				   <td align='right' valign='top' class='required_field'>
Trumpai apie nuorodą
				   </td>
				   <td class='element_label'>
<textarea name='apie' cols='25' rows='5' style='width: 200px;'></textarea>
				   </td>

				</tr>
				<tr>
				   <td align='right' class='normal_field'>
Svetaines logo
				   </td>
				   <td class='element_label'>
					  <input type='text' name='pic' value='http://' size='20' style='width: 200px;'>
				   </td>
				</tr>
				<tr>

				   <td colspan='2'>
					  <hr>
					  </hr>
				   </td>
				   <tr>
					  <td colspan='2' align='center'>
						 <input type='submit' name='Submit_link' value='Siųsti'>
					  </td>
				   </tr>

				</tr>
			 </table>
		  </td>
	   </tr>
	</table>
 </form>
";
	hide("Įdėti naują nuorodą",$text,true);
}
?>